package com.product.common.utils.QRcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.product.common.utils.uuid.UUID;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class QRCodeUtils {
    public String getQRcodeImage(String uuid) throws WriterException, IOException {
        //设置二维码图片的长宽
        String filePath = "E:\\开发项目\\溯源管理系统\\product_trace\\file\\picture\\";
        int width = 300;
        int height = 300;
        //生成二维码类
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //生成二维码
        BitMatrix bitMatrix = qrCodeWriter.encode(uuid, BarcodeFormat.QR_CODE, width, height);
        //生成图片唯一名称,加上.png格式
        String pat = uuid + ".png";
        //设置图片输出路径
        filePath += pat;
        Path path = FileSystems.getDefault().getPath(filePath);
        //输出二维码图片
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
        return "/profile/picture/" + pat;
    }


}
